const common = {
    indexEmpty: function(...str){
        for(let i = 0;i<str.length;i++){
            if(typeof str[i] == 'number')continue;
            if(typeof str[i] == 'string'&&str[i].trim().length == 0 ){
                return i;
            }
            if (str[i] == null || str[i] == "null" || str[i]==undefined || str[i]=='undefined' ) {
                return i;
            }
            if(Array.isArray(str[i])&&str[i].length==0){
                return i
            }
        }
        return -1;
    },

    isEmpty: function(...str){
        return this.indexEmpty(...str)>-1
    },

    protectName: function(name) {
        let newStr;
        if (name.length === 2) {
            newStr = name.substr(0, 1) + '*';
        } else if (name.length > 2) {
            let char = '';
            for (let i = 0, len = name.length - 2; i < len; i++) {
                if(i<2){
                    char += '*';
                }
            }
            newStr = name.substr(0, 1) + char + name.substr(-1, 1);
        } else {
            newStr = name;
        }
        return newStr;
    },
    protectTel: function(tel) {
        if(this.isEmpty(tel))return ''
        return tel.replace(/^(\d{3})\d{4}(\d+)/,"$1****$2");;
    },
    /**
     * 银行卡脱敏
     * @param card
     * @returns {*}
     */
    protectBank: function(card) {
        return card.replace(/^(\d{4})\d+(\d{4})$/, "$1****$2");
    },
    /**
     * 获取日期范围，一般用于查询数据
     * @param type:week本周，month本月，year本年
     * @returns {{start: string, end: string}}
     */
    getDateArea:function(type){
        let date = new Date();
        let start='';let end='';
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const my_month = month<10?'0'+month:month;
        const day =  date.getDate();
        const my_day = day < 10 ? ('0' + day) : day;
        const today = year+"-"+my_month+"-"+my_day;
        if(type=='week'||type==1){
            date.setDate(date.getDate() - date.getDay() + 1);
            let monday = date.getDate();
            let monday_month = date.getMonth()+1;
            start =date.getFullYear() + "-" + (monday_month<10?'0'+monday_month:monday_month) + "-" +( monday < 10 ? ('0' + monday) : monday);
            date.setDate(date.getDate() + 6);
            let sunday_month = date.getMonth()+1;
            let sunday = date.getDate();
            end = date.getFullYear() + "-" + (sunday_month<10?'0'+sunday_month:sunday_month) + "-" +  (sunday < 10 ? ('0' + sunday) : sunday);
        }else if (type=='month'||type==2){
            start = year+"-"+my_month+"-01";
            end = year+"-"+my_month+"-"+new Date(year,my_month,0).getDate();
        }else if(type=='year'||type==3){
            start = year+"-01-01";
            end = year+"-12-31";
        }else{
            start = today;
            end = today;
        }
        return {start:start,end:end};
    },
    /**
     * 时间转换为中文单位
     * @param total
     * @param type
     * @returns {string}
     */
    formatDuration:function(total,type){
        if(isNaN(total)){
            return '非法数字'
        }
        let duration = "";
        if(type=='h'){
            let hour = parseInt(total/60);
            let min = total%60;
            if(hour>0)duration+=hour+"小时";
            if(min>=0)duration+=min+"分钟";
            return duration;
        }
        total = total*60;
        let day = parseInt(total/ (24*60*60));//计算整数天数
        let afterDay = total - day*24*60*60;//取得算出天数后剩余的秒数
        let hour = parseInt(afterDay/(60*60));//计算整数小时数
        let afterHour = total - day*24*60*60 - hour*60*60;//取得算出小时数后剩余的秒数
        let min = parseInt(afterHour/60);//计算整数分
        let afterMin = total - day*24*60*60 - hour*60*60 - min*60;//取得算出分后剩余的秒数
        if(day>0)duration+=day+"天";
        if(hour>0)duration+=hour+"小时";
        if(min>=0)duration+=min+"分钟";
        return duration;
    },
    /**
     * 时间补零
     * @param t
     * @returns {string|number}
     */
    formatDate:function (t) {
        t = t || Date.now();
        let time = new Date(t);
        let str = time.getMonth() < 9 ? ('0' + (time.getMonth() + 1)) : (time.getMonth() + 1);
        str += '-';
        str += time.getDate() < 10 ? ('0' + time.getDate()) : time.getDate();
        str += ' ';
        str += time.getHours();
        str += ':';
        str += time.getMinutes() < 10 ? ('0' + time.getMinutes()) : time.getMinutes();
        return str;
    },
    /**
     * 获取日期对应的星座
     * @param t
     * @returns {string}
     */
    getAstro:function(t){
        t = new Date(t)
        let m = t.getMonth()
        let d = t.getDate()
        return "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯".substr(m*2-(d<"102223444433".charAt(m-1)- -19)*2,2);
    },
    /**
     * 获取经纬度的距离
     * @param lat1
     * @param lng1
     * @param lat2
     * @param lng2
     * @returns {number}
     */
    getDistance:function(lat1,lng1,lat2,lng2){
        var radLat1 = lat1*Math.PI / 180.0;
        var radLat2 = lat2*Math.PI / 180.0;
        var a = radLat1 - radLat2;
        var  b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
        var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
            Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s *6378.137 ;// EARTH_RADIUS;
        s = Math.round(s * 10000) / 10;
        return s;
    },
    /**
     * 数字转换为距离
     * @param num
     * @returns {string|number}
     */
    getNumberTransform:function(num){
        if(typeof num!='number'){
            return 0;
        }
        if(num>10000){
            return (num/10000)+'w';
        }
        return num;
    },
    /**
     * 随机颜色
     * @returns {`#${string}`}
     */
    randomColor: function(){
        return `#${Math.floor(Math.random() * 0xffffff).toString(16)}`
    },
    /**
     * 数组洗牌
     * @param arr
     * @returns array
     */
    listShuffle:function(arr){
        return arr.sort(() => Math.random() - 0.5)
    },
    /**
     * 休眠函数
     * @param ms
     * @returns {Promise<unknown>}
     */
    sleep:function(ms){
        return new Promise((resolve)=> setTimeout(resolve, ms))
    }
}


export {common}